package com.codingvip.sort;

public class SelectionSort {
    public static void main(String[] args) {
        int a[] = {5, 8, 3, 4, 2};
        //1 选择排序
//        selectSort(a);
        //2 插入排序
        //
        insertSort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }

    }

    private static void insertSort(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int tmp = a[i];
            int updateIndex = i;
            for (int j = i-1; j>=0; j--) {
                if (a[j] > tmp) {
                    a[j + 1] = a[j];
                    updateIndex = j;
                }else{
                    System.out.println("Break:::When==" + j);
                    break;
                }
            }
            if (updateIndex != i) {
                a[updateIndex] = tmp;
            }

        }
    }
    /**
     * @param a
     */
    private static void selectSort(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[minIndex]) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                int tmp = a[i];
                a[i] = a[minIndex];
                a[minIndex] = tmp;
            }
        }
    }

}
